<?php

/** 
 * @author Boo
 * 
 * 
 */
include_once 'library/Auth/Auth.php';
include_once 'library/Controller/IController.php';
class BaseController implements IController {
	const SURFIX = 'Action';
	/**
	 *
	 * @var String get type insert or update
	 */
	protected $_actionType;
	/**
	 *
	 * @var String action name
	 */
	private $_actionName;
	/**
	 * Controller Name
	 *
	 * @var string
	 */
	protected $_context;
	/**
	 *
	 * @var BaseModel
	 */
	protected $_model;
	/**
	 *
	 * @var array
	 */
	public $_data;
	/**
	 * View name
	 *
	 * @var String
	 */
	private $_viewType;
	/**
	 * View Render
	 *
	 * @var ViewRender
	 */
	private $_view;
	/**
	 *
	 * @var Trip
	 */
	public $trip;
	/**
	 */
	function __construct() {
		if (isset ( $_REQUEST [Config::ACTION_PARRAM_NAME] )) {
			$this->_actionName = $_REQUEST [Config::ACTION_PARRAM_NAME];
		} else {
			$this->_actionName = Config::ACTION_DEFAULT;
		}
		if (isset ( $_REQUEST [Config::VIEW_PARRAM_NAME] )) {
			$this->_viewType = $_REQUEST [Config::VIEW_PARRAM_NAME];
		} else {
			$this->_viewType = Config::VIEW_DEFAULT;
		}
		$this->_init ();
	}
	
	/*
	 * (non-PHPdoc) @see IController::_init()
	 */
	public function _init() {
		// TODO Auto-generated method stub
		$this->getDataAction ();
		$this->trip = new Trip ();
	}
	
	/**
	 * main Controller Runtime.
	 */
	public function run() {		
		$name = $this->_actionName . BaseController::SURFIX;
		if (method_exists ( get_class ( $this ), $name )) {
			$this->_view = new ViewRender ( $this->_context, $this->_actionName, $this->_viewType );
			$this->{$name} ();
			$this->_view->render ();
		} else {
			throw new ControllerException ( "Method " . $name . ' not found!' );
		}
	}
	/**
	 *
	 * @return the $_context
	 */
	public function getContext() {
		return $this->_context;
	}
	
	/**
	 *
	 * @param string $_context        	
	 */
	public function setContext($_context) {
		$this->_context = $_context;
		$this->_model = ModelFactory::instance ( $_context );
	}
	
	/* (non-PHPdoc)
	 * @see IController::block()
	 */
	public function block($blockname) {
		// TODO Auto-generated method stub
		echo BLOCK_PATH . '/'.$blockname.'.php';
	}

	/*
	 * (non-PHPdoc) @see IController::getAllAction()
	 */
	public function getallAction() {
		// TODO Auto-generated method stub
		$start = (isset($_GET['start']) && (int)$_GET['start'])?$_GET['start']:0;
		if ($this->_context == 'Point')
			$strlimit = 'limit '.$start.',14 ';
		else 
			$strlimit = 'limit '.$start.',10 ';
		$this->view ()->object = $this->_model->getAll ($strlimit);
		echo '---'.$strlimit;
		$this->view ()->title = Resource::getResource('list') ." ".Resource::getResource($this->_context) ;
	}
	/*
	 * (non-PHPdoc) @see IController::getByIDAction()
	 */
	public function getbyidAction() {
		// TODO Auto-generated method stub
		$this->view ()->object = $this->_model->getbyid ( $this->_data );
	}
	/*
	 * (non-PHPdoc) @see IController::getIOrUAction()
	 */
	public function getioruAction() {
		// TODO Auto-generated method stub
		if (! isset ( $_GET ["_" . $_GET ['_module'] . "id"] ))
			$this->view ()->object = $this->_model->insert ( $this->_data );
		else
			$this->view ()->object = $this->_model->update ( $this->_data );
	}
	/**
	 * Get data param
	 */
	public function getDataAction() {
		$data = iArray::removeAtTop ( $_GET, 2 );
		foreach ( $data as $key => $value ) {
			$newkey = substr ( $key, strlen ( $_GET ['_module'] ) + 1 );
			$data [$newkey] = $data [$key];
			unset ( $data [$key] );
		}
		if (isset ( $data ['password'] ))
			$data ["password"] = iKey::encrypepass( $data ["password"] );
		$this->_data = $data;
		
	}
	/*
	 * (non-PHPdoc) @see IController::getDeleteAction()
	 */
	public function getdeleteAction() {
		// TODO Auto-generated method stub
		$keyword = array (
				'user',
				'driver',
				'car' 
		);
		$this->_model->delete ( $this->_data, $keyword );
	}
	/*
	 * (non-PHPdoc) @see IController::indexAction()
	 */
	public function indexAction() {
		// TODO Auto-generated method stub
		$this->view ()->object = null;
	}
	
	/**
	 *
	 * @return the $_actionName
	 */
	public function getActionName() {
		return $this->_actionName;
	}
	
	/**
	 *
	 * @param field_type $_actionName        	
	 */
	public function setActionName($_actionName) {
		$this->_actionName = $_actionName;
	}
	/**
	 * test action
	 */
	public function testAction() {
		$date = '2014-10-05 05:37:20';
		iDate::getNowdate($date);
	}
	/**
	 *
	 * @return ViewRender
	 */
	public function view() {
		return $this->_view;
	}
	
	/**
	 * @return the $_viewType
	 */
	public function getViewType() {
		return $this->_viewType;
	}

	/**
	 * @param string $_viewType
	 */
	public function setViewType($_viewType) {
		$this->_viewType = $_viewType;
	}

	/**
	 *
	 * @param ViewRender $_view        	
	 */
	public function setView($_view) {
		$this->_view = $_view;
	}
	/*
	 * (non-PHPdoc) @see IController::editAction()
	 */
	public function editAction() {
		// TODO Auto-generated method stub
		if ($this->isPost ()) {
			$entity = $this->getEntityData ();
			iDebug::debugArray ( $entity );
			switch ($this->_actionType) {
				case 'insert' :
					echo 'insert';
					$this->view ()->objectData = $this->_model->insert ( $entity );
					break;
				case 'update' :
					echo 'update';
					$this->view ()->objectData = $this->_model->update ( $entity );
					break;
			}
			 $this->_redirect ( '?_module=' . $this->_context . '&_action=getall' );
		} else {
			$id = $this->_model->getId ();
			$entity = array ();
			foreach ( $id as $value ) {
				$param = '_' . $this->_context . $value;
				if ($this->getParram ( $param, '' ) != '')
					$entity [$value] = $this->getParram ( $param, '' );
			}
			if (count ( $entity ) > 0) {
				$this->view ()->objectData = $this->_model->getbyid ( $entity );
				$this->view ()->title = Resource::getResource('edit') ." ".Resource::getResource($this->_context) ;
			}
			if (count ( $entity ) == 0) {
				$this->view ()->title = Resource::getResource('insert') ." ".Resource::getResource($this->_context) ;
			}
		}
	}
	public function getParram($key, $default = null) {
		return isset ( $_REQUEST [$key] ) ? $_REQUEST [$key] : $default;
	}
	/**
	 * 
	 * @return entity
	 */
	public function getEntityData() {
		$entity = array ();
		foreach ( $_REQUEST as $key => $value ) {
			if (! strcmp ( $key, 'actiontype' )) {
				$this->_actionType = $value;
			}
			$arr = explode ( '_', $key );
			if (count ( $arr ) == 2 && $arr [0] == $this->_context) {
				$kk = $arr [1];
				$entity [$kk] = $value;
			}
		}
		return $entity;
	}
	/**
	 * Check request is post or get.
	 *
	 * @return true or false
	 */
	public function isPost() {
		return ($_SERVER ['REQUEST_METHOD'] == 'POST') ? true : false;
	}
	/**
	 * Redirect to page.
	 *
	 * @param String $url        	
	 * @param int $num        	
	 */
	function _redirect($url, $internal = true, $num = null) {
		static $http = array (
				100 => "HTTP/1.1 100 Continue",
				101 => "HTTP/1.1 101 Switching Protocols",
				200 => "HTTP/1.1 200 OK",
				201 => "HTTP/1.1 201 Created",
				202 => "HTTP/1.1 202 Accepted",
				203 => "HTTP/1.1 203 Non-Authoritative Information",
				204 => "HTTP/1.1 204 No Content",
				205 => "HTTP/1.1 205 Reset Content",
				206 => "HTTP/1.1 206 Partial Content",
				300 => "HTTP/1.1 300 Multiple Choices",
				301 => "HTTP/1.1 301 Moved Permanently",
				302 => "HTTP/1.1 302 Found",
				303 => "HTTP/1.1 303 See Other",
				304 => "HTTP/1.1 304 Not Modified",
				305 => "HTTP/1.1 305 Use Proxy",
				307 => "HTTP/1.1 307 Temporary Redirect",
				400 => "HTTP/1.1 400 Bad Request",
				401 => "HTTP/1.1 401 Unauthorized",
				402 => "HTTP/1.1 402 Payment Required",
				403 => "HTTP/1.1 403 Forbidden",
				404 => "HTTP/1.1 404 Not Found",
				405 => "HTTP/1.1 405 Method Not Allowed",
				406 => "HTTP/1.1 406 Not Acceptable",
				407 => "HTTP/1.1 407 Proxy Authentication Required",
				408 => "HTTP/1.1 408 Request Time-out",
				409 => "HTTP/1.1 409 Conflict",
				410 => "HTTP/1.1 410 Gone",
				411 => "HTTP/1.1 411 Length Required",
				412 => "HTTP/1.1 412 Precondition Failed",
				413 => "HTTP/1.1 413 Request Entity Too Large",
				414 => "HTTP/1.1 414 Request-URI Too Large",
				415 => "HTTP/1.1 415 Unsupported Media Type",
				416 => "HTTP/1.1 416 Requested range not satisfiable",
				417 => "HTTP/1.1 417 Expectation Failed",
				500 => "HTTP/1.1 500 Internal Server Error",
				501 => "HTTP/1.1 501 Not Implemented",
				502 => "HTTP/1.1 502 Bad Gateway",
				503 => "HTTP/1.1 503 Service Unavailable",
				504 => "HTTP/1.1 504 Gateway Time-out" 
		);
		if ($num != null) {
			header ( $http [$num] );
		}
		if ($internal && $this->_context != Config::CONTROLLER_DEFAULT) {
			$url = Config::BASEURL . $url ;
		}
		header ( "Location: " . $url );
	}
	/*
	 * (non-PHPdoc) @see IController::changeselloffAction()
	 */
	public function changeselloffAction() {

	}
	/*
	 * (non-PHPdoc) @see IController::updatestatusAction()
	 */
	public function updatestatusAction() {
		// TODO Auto-generated method stub
		$id = $this->getParram ( '_id', '0' );
		$status = $this->getParram ( '_status', '0' );
		$this->result = $this->_model->updatestatus ( $id, $status );
	}
}
?>